package cn.bj.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bj.beans.Yxhq_work_picture;
import cn.bj.service.YxhqWorkPictureService;

/*
 * 优选婚庆作品集控制层
 * 
 * */
@Controller
@RequestMapping("/admin")
public class YxhqWorkPictureController {
	@Autowired
	YxhqWorkPictureService yxhqWorkPictureService;

	// 查询所有
	/*
	 * @RequestMapping("/") public String getAllYxhqWorkPicture (){
	 * List<Yxhq_work_picture> yxhqWPs =
	 * yxhqWorkPictureService.getAllYxhqWorkPicture(); return ""; }
	 */

	// 增加
	@ResponseBody
	@PostMapping(value = "/insertYxhqWP.do") // 参数放入MultipartFile接口，引用放入jsp页面上传文件的name属性
	public String insertYxhqWP(HttpServletRequest request, MultipartFile fd2, MultipartFile fd3, HttpSession session)
			throws IllegalArgumentException, IOException {

		// 返回信息
		String msg;

		if (null != fd2 && null != fd3) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxhqImg");
			String path2 = session.getServletContext().getRealPath("/YxhqImg");

			// 文件名以原来文件名命名
			String ImgName = fd2.getOriginalFilename();
			String ImgName2 = fd3.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if ((ImgName.endsWith("jpg") || ImgName.endsWith("png"))
					&& (ImgName2.endsWith("jpg") || ImgName2.endsWith("png"))) {
				// 装载文件
				File file = new File(path, ImgName);
				File file2 = new File(path, ImgName2);

				// 上传文件
				fd2.transferTo(file);
				fd3.transferTo(file2);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName;
				path2 = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName2;

				// 获取前端页面传过来数据
				String targetUrl = request.getParameter("targetUrl");// 标题

				// 创建
				Yxhq_work_picture yxhqWP = new Yxhq_work_picture();
				yxhqWP.setTargetUrl(targetUrl);
				yxhqWP.setwPicture(path);
				yxhqWP.setP1(path2);

				int num4 = yxhqWorkPictureService.insertYxhqWp(yxhqWP);
				if (num4 != 0) {
					msg = "添加成功";
					System.out.println(msg);
					return msg;
				} else {
					msg = "添加失败";
					return msg;
				}

			}

		} else if (null != fd2) {// 如果只有产品图，没有表格图

			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxhqImg");

			// 文件名以原来文件名命名
			String ImgName = fd2.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if (ImgName.endsWith("jpg") || ImgName.endsWith("png")) {
				// 装载文件
				File file = new File(path, ImgName);

				// 上传文件
				fd2.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName;

				// 获取前端页面传过来数据
				String targetUrl = request.getParameter("targetUrl");// 标题

				// 创建
				Yxhq_work_picture yxhqWP = new Yxhq_work_picture();
				yxhqWP.setTargetUrl(targetUrl);
				yxhqWP.setwPicture(path);

				int num4 = yxhqWorkPictureService.insertYxhqWp(yxhqWP);

				if (num4 != 0) {
					msg = "添加成功";
					System.out.println(msg);
					return msg;
				} else {
					msg = "添加失败";
					return msg;
				}
			}
			msg = "图片格式不正确，请确认图片格式jpg/png";
			return msg;
		}
		msg = "文件不能为空";
		return msg;
	}

	// 删除
	@RequestMapping("/deleteYxhqWP.do")
	public String deleteYxhqWP(@Param("id") Integer id) {
		int num5 = yxhqWorkPictureService.deleteByYxhqWP(id);
		System.out.println(num5);
		return "redirect:/admin/AllAdmins.do";
	}

	// 跳转修改图片,拿到单个对象
	@RequestMapping("/GotoUpdateYxhqWP.do")
	public String GotoUpdatePicture(@Param("id") Integer id, Model model) {
		Yxhq_work_picture ywp = yxhqWorkPictureService.selectById(id);
		model.addAttribute("ywp", ywp);
		return "admin/updateYxhqWP";
	}

	// 修改
	@PostMapping(value = "/YxhqWPUpdate.do") // 参数放入MultipartFile接口，引用放入jsp页面上传文件的name属性
	public String updateYxhqWP(@Param("id") Integer id, Yxhq_work_picture ywp, HttpServletRequest request,
			MultipartFile img, MultipartFile img2, HttpSession session) throws IllegalArgumentException, IOException {

		if (null != img && null != img2) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxhqImg");
			String path2 = session.getServletContext().getRealPath("/YxhqImg");

			// 文件名以原来文件名命名
			String ImgName = img.getOriginalFilename();
			String ImgName2 = img2.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if ((ImgName.endsWith("jpg") || ImgName.endsWith("png"))
					&& (ImgName2.endsWith("jpg") || ImgName2.endsWith("png"))) {
				// 装载文件
				File file = new File(path, ImgName);
				File file2 = new File(path, ImgName2);

				// 上传文件
				img.transferTo(file);
				img2.transferTo(file2);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName;
				path2 = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName2;

				// 获取前端页面传过来数据
				// String targetUrl = request.getParameter("targetUrl");// 标题

				// 创建
				ywp.setwPicture(path);
				ywp.setP1(path2);
				ywp.setId(id);

				int num4 = yxhqWorkPictureService.updateYxhqWp(ywp);
				if (num4 != 0) {
					return "redirect:/admin/AllAdmins.do";
				} else {
					return "admin/error2";
				}

			}

		} else if (null != img) {// 如果只有产品图，没有表格图

			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxhqImg");

			// 文件名以原来文件名命名
			String ImgName = img.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if (ImgName.endsWith("jpg") || ImgName.endsWith("png")) {
				// 装载文件
				File file = new File(path, ImgName);

				// 上传文件
				img.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxhqImg/" + ImgName;

				// 获取前端页面传过来数据
				// String targetUrl = request.getParameter("targetUrl");// 标题

				// 修改
				ywp.setwPicture(path);
				int num4 = yxhqWorkPictureService.insertYxhqWp(ywp);

				if (num4 != 0) {
					return "redirect:/admin/AllAdmins.do";
				} else {
					return "admin/error2";
				}
			}
			return "admin/error2";
		}
		return "admin/error2";
	}

}
